<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicTableCols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsInitTable="true"
      :dynamicIsColumnDrop="true"
    >
      <div slot="isEnable"></div>
      <template slot="controlBtn" slot-scope="scope">
        <el-button
          type="primary"
          size="mini"
          class="el-icon-edit"
          @click="edit(scope.row.index)"
          >编辑</el-button
        >
      </template>
    </one-table-template>
    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :is-show-chooes="isShowChooes"
      @close="close"
    />
  </div>
</template>

<script>
import OneTableTemplate from "@/components/wms/templates/oneTable";
import { basedataApi } from "@/api/wms/basedata";
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import requesta from "../../../../utils/request.js";
export default {
  components: {
    OneTableTemplate,
    Edit: () => import("./edit")
  },
  mixins: [oneTableWithViewTemplateMixins],
  data() {
    return {
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          name: "search",
          position: "right",
          clickEvent: () => this.queryTable(1),
          text: this.$t("sys.button.query") /*查5555询*/
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: this.$t("sys.button.reset") /*重置*/
        },
        {
          compKey: "btnKey3",
          type: "add",
          size: "small",
          name: "add",
          position: "left",
          clickEvent: () => this.add(),
          text: "新增" /*导出*/
        },
        {
          compKey: "btnKey4",
          type: "export",
          size: "small",
          name: "export",
          position: "left",
          clickEvent: () => this.export(),
          text: "导出" /*导出*/
        },
        {
          compKey: "btnKey6",
          type: "",
          size: "small",
          name: "importTemplateDownload",
          position: "left",
          clickEvent: () => this.downloadTemplate(),
          text: "模板下载" //"模板下载"
        },
        {
          compKey: "btnKey5",
          text: "导入",
          category: "ve",
          codeField: "fileChannel",
          position: "upload",
          isMust: true
        } //导入
      ],
      tableComponents: [
        {
          compKey: "compKey2",
          labelName: "用户编号" /*厂商名称*/,
          codeField: "WAREHOUSE_USER_ID",
          component: () => import("@/components/wms/commonInput"),
          type: "dropdownList",
          isMust: true
        },
        {
          compKey: "compKey1",
          labelName: "用户名称",
          codeField: "EMP_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey6",
          labelName: "状态", // '状态',
          codeField: "IS_ENABLE",
          component: () => import("@/components/wms/isEnable/isEnableOne"),
          type: "dropdownList",
          isMust: true
        }
      ],
      tableCols: this.getCols(),
      //表单查询数据
      formField: {
        WAREHOUSE_USER_ID: "",
        EMP_NAME: "",
        IS_ENABLE: ""
      },
      isShowChooes: false,
      apiConfig: basedataApi.querywarehouseuser
    };
  },
  created() {},
  methods: {
    add() {
      // this.isShowChooes = true;
      this.showEdit("add");
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      requesta({
        url: "/mp/wm/vl/basedata/warehouseuser/exportwarehouseuser.do",
        method: "POST",
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "仓库用户管理.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.info(err);
        });
    },
    downloadTemplate() {
      let downloadElement = document.createElement("a");
      let href = "/wms/fileTemplate/仓库用户管理模板.xlsx"; // 创建下载的链接
      downloadElement.href = href;
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
    },
    handFileListChange(file, fileList) {},
    // 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeAvatarUpload(file) {
      if (file.size > 50 * 1024 * 1024) {
        this.$message({
          message: "文件大小不能超过50M",
          type: "warning"
        });
        return false;
      }
      let fileType = file.name.substr(file.name.lastIndexOf(".") + 1);
      if (fileType != "xls" && fileType != "xlsx") {
        this.$message({
          message: "只能导入后缀为xls,xlsx的文件！",
          type: "warning"
        });
        return false;
      }
      return true;
    },
    handleChange(param) {
      const loading = this.$loading({
        lock: true,
        text: "导入中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      const that = this;
      const url =
        "/mp/wm/vl/basedata/warehouseuser/importExcelTowarehouseuser.do";
      that
        .$requestUpload(url, param)
        .then(response => {
          loading.close();
          if (response.result === "1") {
            this.$message({
              message: "导入成功",
              type: "success"
            });
            that.queryTable();
          } else {
            this.$message({
              message: response.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {});
    },
    getCols() {
      var cols = [
        {
          prop: "controlBtn",
          label: "操作", //"操作"
          codeField: "controlBtn",
          width: 80,
          align: "center",
          isSlot: true
        }
      ];
      cols = cols.concat([
        {
          prop: "WAREHOUSE_USER_ID",
          label: "用户编号",
          width: null,
          align: "center"
        },
        {
          prop: "EMP_NAME",
          label: "用户名称",
          width: null,
          align: "center"
        },
        {
          prop: "WAREHOUSE_NAME",
          label: "所属仓库",
          width: null,
          align: "center"
        },
        {
          prop: "WAREHOUSE_ID",
          label: "WAREHOUSE_ID",
          hidden: true,
          align: "center"
        },
        {
          prop: "IS_ENABLE_STR",
          label: "状态",
          width: null,
          align: "center"
        }
      ]);
      return cols;
    }
  }
};
</script>

<style lang="scss" scoped>
// .warehouseUser {
//   margin: 10px;
//   background: white;
//   padding: 10px;
// }
</style>
